//
//  UIImageView+LLNW.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年 . All rights reserved.
//

#import "UIImageView+LLNW.h"
#import "UIImageView+AFNetworking.h"
#import "LLNWHTTPFileHandler.h"

@implementation UIImageView (LLNW)

- (void)llnw_setImageWithURL:(NSURL *)url
{
    
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setImage:image];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    
    __weak UIImageView * weakSelf = self;
    
    [self setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setImage:image];
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        
        
        
    }];
    
}

- (void)llnw_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage
{
    
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setImage:image];
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    __weak UIImageView * weakSelf = self;
    
    [self setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setImage:image];
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    }];
}

- (void)llnw_setImageWithURL:(NSURL *)url
            placeholderImage:(UIImage *)placeholderImage
                     success:(void (^)(UIImage *image))success
                     failure:(void (^)(NSError *error))failure;
{
    
    UIImage * image = [[LLNWHTTPFileHandler shareInstance] getImageWithUrl:url];
    
    if(image){
        
        [self setImage:image];
        
        success(image);
        
        return;
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    __weak UIImageView * weakSelf = self;
    
    [self setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [[LLNWHTTPFileHandler shareInstance] saveImage:image withUrl:url];
        
        [weakSelf setImage:image];
        
        success(image);
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        
        failure(error);
    }];
}

- (void)llnw_cancelImageDownloadTask
{
    
    [self cancelImageDownloadTask];
}

@end
